home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / gfx / misc / FujiControl.lha / CameraCommands.c < prev    next >
Encoding:
Text File  |  1999-06-06  |  12.4 KB  |  459 lines

  1. /************ Camera commands ************/
  2.  
  3. /***** get_num_pics() - returns the number of pictures stored in the camera *****/
  4. get_num_pics()
  5. {
  6.     unsigned char txbuf[4],rxbuf[8];
  7.     int chars_received;
  8.  
  9.     if (debug & 32) printf("»»Get number of images : result =");
  10.     txbuf[0]=0x00;    txbuf[1]=0x0b;    txbuf[2]=0x00;    txbuf[3]=0x00;
  11.     sendpacket(txbuf,4);    chars_received=receivepacket(rxbuf,8);
  12.     if (chars_received != 2) {    if (debug & 32) printf("ERROR!\n"); return(0); }
  13.     if (debug & 32) printf(" %d\n",rxbuf[0] + 256 * rxbuf[1]);
  14.     return( rxbuf[0] + 256 * rxbuf[1] );
  15. }
  16.  
  17. /***** get_image_size(image no) - returns the size of an image in the camera *****/
  18. get_image_size(int imageno)
  19. {
  20.     unsigned char txbuf[8],rxbuf[10];
  21.     int chars_received;
  22.  
  23.     if (debug & 32) printf("»»Get size of image %d : result =",imageno);
  24.     txbuf[0]=0x00;                
  25.     txbuf[1]=0x17;                    /* command */
  26.     txbuf[2]=0x02; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  27.     txbuf[4]=imageno & 0xff;
  28.     txbuf[5]=imageno >>8;
  29.     sendpacket(txbuf,6);
  30.     chars_received=receivepacket(rxbuf,9);
  31.     if (chars_received != 4)
  32.     {    if (debug &32) printf("!!Error\n"); return(0);}
  33.     return( rxbuf[0] + (rxbuf[1]<<8) + (rxbuf[2]<<16) + (rxbuf[3]<<24) );
  34. }
  35.  
  36. get_image_name(int imageno, char *rxbuf)
  37. {
  38.     unsigned char txbuf[8];
  39.     int chars_received;
  40.  
  41.     if (debug & 32) printf("»»Get name of image %d :",imageno);
  42.     txbuf[0]=0x00;                
  43.     txbuf[1]=0x0a;                    /* command */
  44.     txbuf[2]=0x02; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  45.     txbuf[4]=imageno & 0xff;
  46.     txbuf[5]=imageno >>8;
  47.     sendpacket(txbuf,6);
  48.     chars_received=receivepacket(rxbuf,55);
  49.     if (chars_received < 2)
  50.     {    if (debug &32) printf("!!Error\n"); return(0);}
  51.     rxbuf[chars_received]=NULL;
  52.     if (debug & 32) printf(" result =%s\n", rxbuf);
  53.     return(1);
  54. }
  55.  
  56. get_image_info(int imageno, char *rxbuf)
  57. {
  58.     unsigned char txbuf[8];
  59.     int chars_received;
  60.  
  61.     if (debug & 32) printf("»»Get info for image %d :",imageno);
  62.     txbuf[0]=0x00;                
  63.     txbuf[1]=0x00;                    /* command */
  64.     txbuf[2]=0x02; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  65.     txbuf[4]=imageno & 0xff;
  66.     txbuf[5]=imageno >>8;
  67.     sendpacket(txbuf,6);
  68.     chars_received=receivepacket(rxbuf,10000);
  69.     if (chars_received < 1)
  70.     {    if (debug &32) printf("!!Error\n"); return(0);}
  71.     rxbuf[chars_received]=NULL;
  72.     if (debug & 32) printf(" result =%s\n", rxbuf);
  73.     return(chars_received);
  74. }
  75.  
  76. /***** send_image(filename on disk, name on camera) - uploads image to camera *****/
  77. #define MEMALLOC (1024*1024)
  78. send_image(char * filename, char * imagename)
  79. {
  80.     unsigned char *image;
  81.     int filesize=0;    
  82.     FILE *fp;
  83.     unsigned char command[4];
  84.     char buff[255];
  85.     int i;
  86.  
  87.     printf("»» Send image %s to camera image %s\n",filename,imagename);
  88.  
  89.     /* allocate memory for the image */
  90.     image = (unsigned char *)calloc(MEMALLOC,1);
  91.     if (image==NULL) return(0*printf("!! Could not allocate memory\n"));
  92.  
  93.     /* read in the image file */
  94.     fp=fopen(filename,"r");
  95.     if (fp == NULL) {free(image); printf("!! Could not load %s\n",filename);}
  96.     filesize=fread(image, 1, MEMALLOC,fp);
  97.     fclose(fp);
  98.     printf("   Filesize=%d\n",filesize);
  99.  
  100.     /* send the image name to the camera */
  101.     /* Set the name for picture to upload */
  102.     buff[0]=0;
  103.     buff[1]=0x0f;
  104.     buff[2]=strlen(imagename);
  105.     buff[3]=0;
  106.     for (i=0; i<strlen(imagename); i++)
  107.         buff[4+i]=imagename[i];
  108.     printhex("Name> ",buff,4+strlen(imagename));
  109.     printascii("Name> ",buff,4+strlen(imagename));
  110.     sendpacket(buff,4+strlen(imagename));
  111.     receivepacket(buff,10);
  112.     printhex("Resp<",buff,16);
  113.     printascii("Resp<",buff,16);
  114.  
  115.     /* send the image */
  116.     command[0]=0; command[1]=0x0e; command[2]=0; command[3]=0;
  117.     senddata(image,command,filesize+4);
  118.  
  119.     free (image);
  120.  
  121.     return(1);
  122. }
  123.     
  124.     
  125.  
  126.  
  127. /***** get_image(image no, filename to save) - downloads an image from camera to disk file *****/
  128. get_image(int image_no,char *filename)
  129. {
  130.     unsigned char txbuf[8], *rxbuf;
  131.     int chars_received;
  132.     int image_size;
  133.     FILE *fp;    
  134.  
  135.     image_size=get_image_size(image_no);
  136.     if (!image_size) return(0*printf("!!Image %d contains no picture to download into %s\n",image_no,filename));
  137.  
  138.     if (debug & 32) printf("»»Download image %d ",image_no);
  139.  
  140.     rxbuf=(unsigned char *)calloc(image_size+2000,1);
  141.     if (!rxbuf) return(0*printf("!!Could not allocate %d bytes for downloading image %d into %s\n",image_size,image_no,filename));
  142.  
  143.     fp=fopen(filename,"w");
  144.     if (!fp) 
  145.     {
  146.         printf("!!Could not open file %s for saving image %d\n",filename,image_no);
  147.         if (rxbuf) free(rxbuf);
  148.         return(0);
  149.     }
  150.  
  151.     txbuf[0]=0x00;                
  152.     txbuf[1]=0x02;                    /* command */
  153.     txbuf[2]=0x02; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  154.     txbuf[4]=image_no & 0xff;
  155.     txbuf[5]=image_no >>8;
  156.     sendpacket(txbuf,6);
  157.  
  158.     chars_received=receivepacket(rxbuf,image_size+1000);
  159.     if (chars_received != image_size)
  160.         printf("!!Error in image size: only %d out of expected %d characters received\n",chars_received,image_size);
  161.  
  162.     fwrite( rxbuf, image_size,1,fp);
  163.  
  164.     if (debug &32) printf("   : result =OK\n");
  165.  
  166.     fclose(fp);
  167.     free(rxbuf);
  168.     return(chars_received);
  169. }
  170.  
  171.  
  172.  
  173. /***** get_thumbnail(image no, filename to save) - downloads an image from camera to disk file *****/
  174. get_thumbnail(int image_no,char *filename)
  175. {
  176.     unsigned char txbuf[8], *rxbuf;
  177.     int chars_received;
  178.     int image_size;
  179.     FILE *fp;    
  180.  
  181.     image_size=get_image_size(image_no);
  182.     if (!image_size) return(0*printf("!!Image %d contains no picture to download into %s\n",image_no,filename));
  183.  
  184.     if (debug & 32) printf("»»Download image %d ",image_no);
  185.  
  186.     rxbuf=(unsigned char *)calloc(image_size+2000,1);
  187.     if (!rxbuf) return(0*printf("!!Could not allocate %d bytes for downloading image %d into %s\n",image_size,image_no,filename));
  188.  
  189.     fp=fopen(filename,"w");
  190.     if (!fp) 
  191.     {
  192.         printf("!!Could not open file %s for saving image %d\n",filename,image_no);
  193.         if (rxbuf) free(rxbuf);
  194.         return(0);
  195.     }
  196.  
  197.     txbuf[0]=0x00;                
  198.     txbuf[1]=0x00;                    /* command */
  199.     txbuf[2]=0x02; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  200.     txbuf[4]=image_no & 0xff;
  201.     txbuf[5]=image_no >>8;
  202.     sendpacket(txbuf,6);
  203.  
  204.     chars_received=receivepacket(rxbuf,image_size+1000);
  205.     if (chars_received < 500)
  206.         printf("!!Error in image size: only %d out of expected %d characters received\n",chars_received,image_size);
  207.  
  208.     fwrite( rxbuf, chars_received,1,fp);
  209.  
  210.     if (debug &32) printf("   : result =OK\n");
  211.  
  212.     fclose(fp);
  213.     free(rxbuf);
  214.     return(chars_received);
  215. }
  216.  
  217.  
  218.  
  219. /***** shoot(int *frame,int * status) shoot a picture - returns frame number and status in supplied pointers. Frame also in return value*****/
  220. shoot(int *frame,int * status)
  221. {
  222.     unsigned char txbuf[8],rxbuf[10];
  223.     int chars_received;
  224.  
  225.     if (debug & 32) printf("»»Shoot a picture : ");
  226.     txbuf[0]=0x00;                
  227.     txbuf[1]=0x27;                    /* command */
  228.     txbuf[2]=0x00; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  229.     sendpacket(txbuf,4);
  230.     chars_received=receivepacket(rxbuf,9);
  231.     if (chars_received != 4)
  232.     {    if (debug &32) printf("!!Error\n"); return(0);}
  233.  
  234.     *frame = rxbuf[0] + (rxbuf[1]<<8); *status = rxbuf[2] + (rxbuf[3]<<3);
  235.  
  236.     if (debug & 32) printf("result : frame=%d  status=%04x\n",*frame,*status);
  237.     return( *frame);
  238. }
  239.  
  240. charge_flash(int brightness)
  241. {
  242.     unsigned char txbuf[8],rxbuf[10];
  243.     int chars_received,a;
  244.  
  245.     if (debug & 32) printf("»»Charge flash %d ",brightness);
  246.     txbuf[0]=0x00;
  247.     txbuf[1]=0x34;                    /* command */
  248.     txbuf[2]=0x02; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  249.     txbuf[4]=brightness & 0xff;
  250.     txbuf[5]=brightness >>8;
  251.     sendpacket(txbuf,6);
  252.     chars_received=receivepacket(rxbuf,9);
  253.     if (chars_received != 1)
  254.     {    if (debug &32) printf("!!Error\n"); return(0);}
  255.     printf(": result =%d\n",a=rxbuf[0]);
  256.     return( (int)rxbuf[0] );
  257. }
  258.  
  259.  
  260.  
  261. /***** shoot_preview(filename)  - takes a preview shot and downloads it to filename *****/
  262. shoot_preview( char *filename)
  263. {
  264.     unsigned char txbuf[8], *rxbuf;
  265.     int chars_received;
  266.     int image_size=50000;        /* dont have any idea really how much this should be */
  267.     FILE *fp;    
  268.  
  269.     if (debug & 32) printf("»»Shoot and download preview to %s ",filename);
  270.  
  271.     rxbuf=(unsigned char *)calloc(image_size+2000,1);
  272.     if (!rxbuf) return(0*printf("!!Could not allocate %d bytes for downloading preview into %s\n",image_size,filename));
  273.  
  274.     fp=fopen(filename,"w");
  275.     if (!fp) 
  276.     {
  277.         printf("!!Could not open file %s for saving preview\n",filename);
  278.         if (rxbuf) free(rxbuf);
  279.         return(0);
  280.     }
  281.  
  282.     /* shoot the preview */
  283.     txbuf[0]=0x00;                
  284.     txbuf[1]=0x64;                    /* command */
  285.     txbuf[2]=0x00; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  286.     sendpacket(txbuf,4);
  287.  
  288.     chars_received=receivepacket(rxbuf,10);
  289.     if (chars_received != 2)
  290.         printf("!!Error in shooting preview\n");
  291.  
  292.     /* download the preview */
  293.     txbuf[0]=0x00;                
  294.     txbuf[1]=0x62;                    /* command */
  295.     txbuf[2]=0x00; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  296.     sendpacket(txbuf,4);
  297.  
  298.     chars_received=receivepacket(rxbuf,image_size);
  299.     printf("  Received preview of size %d\n",chars_received);
  300.  
  301.     fwrite( rxbuf, chars_received,1,fp);
  302.  
  303.     if (debug &32) printf("   : result =OK\n");
  304.  
  305.     fclose(fp);
  306.     free(rxbuf);
  307.     return(chars_received);
  308. }
  309.  
  310.  
  311. /***** set_flashmode(mode) - sets the flash mode *****/
  312. set_flashmode(int mode)
  313. {
  314.     unsigned char txbuf[8],rxbuf[10];
  315.     int chars_received;
  316.  
  317.     if (debug & 32) printf("»»Set flashmode to %d :",mode);
  318.     txbuf[0]=0x00;                
  319.     txbuf[1]=0x32;                    /* command */
  320.     txbuf[2]=0x01; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  321.     txbuf[4]=mode;
  322.     sendpacket(txbuf,5);
  323.     chars_received=receivepacket(rxbuf,6);
  324.     if (chars_received != 1)
  325.     {    if (debug &32) printf("!!Error\n"); return(0);}
  326.     if (debug & 32) printf(" result=%d\n",(int)rxbuf[0]);
  327.     return( (int)rxbuf[0]);
  328. }
  329.  
  330.  
  331.  
  332. /***** delete_image(image no) - returns the size of an image in the camera *****/
  333. delete_image(int imageno)
  334. {
  335.     unsigned char txbuf[8],rxbuf[10];
  336.     int chars_received;
  337.  
  338.     if (debug & 32) printf("»»Delete image %d :",imageno);
  339.     txbuf[0]=0x00;                
  340.     txbuf[1]=0x19;                    /* command */
  341.     txbuf[2]=0x02; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  342.     txbuf[4]=imageno & 0xff;
  343.     txbuf[5]=imageno >>8;
  344.     sendpacket(txbuf,6);
  345.     chars_received=receivepacket(rxbuf,9);
  346.     if (chars_received != 1 || rxbuf[0])
  347.     {    if (debug &32) printf("!!Error\n"); return(0);}
  348.     if (debug & 32) printf("result = OK\n");
  349.     return( 1 );
  350. }
  351.  
  352. /***** getcommand(command) - get command info byte *****/
  353. getcommandinfo(int command)
  354. {
  355.     unsigned char txbuf[8],rxbuf[10];
  356.     int chars_received;
  357.  
  358.     if (debug & 32) printf("»»Get command info for %02x : ",command);
  359.     txbuf[0]=0x00;
  360.     txbuf[1]=0x51;                    /* command */
  361.     txbuf[2]=0x01; txbuf[3]=0x00;        /* length of data being sent with command (lo/hi bytes) */
  362.     txbuf[4]=command;
  363.     sendpacket(txbuf,5);
  364.     chars_received=receivepacket(rxbuf,9);
  365.     if (chars_received != 2)
  366.     {    if (debug &32) printf("!!Error\n"); return(0);}
  367.  
  368.     printf("&Command %02x -> %02x %02x\n",command,(int)rxbuf[0],(int)rxbuf[1]);
  369.     return( rxbuf[0] + (rxbuf[1]<<8)  );
  370. }
  371.  
  372. #define BAUDRATE 57600
  373. #define BAUD_SEL 7
  374. setup_comm()
  375. {
  376.     unsigned char buffer[512];
  377.     int show=0;
  378.  
  379.      if (debug & 128) show=1;
  380.  
  381.     if (debug & 128) printf(">Trying to establish contact at %d\n",BAUDRATE);
  382.     openserial(BAUDRATE);
  383.     flushserial(buffer,512,show);
  384.     sendchar(0x05);
  385.     sendchar(0x05);
  386.     Delay(5);
  387.     flushserial(buffer,512,show);
  388.     if (buffer[0]==0x6 && buffer[1]==0x6)
  389.     {
  390.         if(debug & 128) printf(" >Speed already %d\n",BAUDRATE);
  391.         return(1);
  392.     }
  393.  
  394.  
  395.     if (debug & 128) printf(" >Failed at %d\n>Trying to establish contact at 9600\n",BAUDRATE);
  396.     closeserial();
  397.     openserial(9600);
  398.     flushserial(buffer,512,show);
  399.     sendchar(0x05);
  400.     sendchar(0x05);
  401.     flushserial(buffer,512,show);
  402.     if (buffer[0]==0x06 && buffer[1]==0x06)
  403.          if (debug & 128) printf(" >Connected succesfully at 9600\n");
  404.     else
  405.     {
  406.         if (debug & 128) printf(" >FAILED\n");
  407.         return(0);
  408.     }
  409.  
  410.     if (debug & 128) printf(">Sending speed change command to camera\n");
  411.     sendchar(0x10);
  412.     sendchar(0x02);
  413.         sendchar(0x01);
  414.         sendchar(0x07);
  415.         sendchar(0x01);
  416.         sendchar(0x00);
  417.          sendchar(BAUD_SEL);
  418.     sendchar(0x10);
  419.     sendchar(0x03);
  420.     sendchar(1^7^1^0^BAUD_SEL^3);
  421.  
  422.     Delay(5);
  423.     flushserial(buffer,512,show);
  424.     if (buffer[0]=0x6)
  425.         if (debug & 128) printf(" >Command acknowledged\n");
  426.     else
  427.     {
  428.         if (debug & 128) printf(" >FAILED\n");
  429.         return(0);
  430.     }
  431.  
  432.     sendchar(0x06);
  433.     flushserial(buffer,512,show);
  434.  
  435.     if (debug &128) printf(">Closing connection\n");
  436.     sendchar(0x04);
  437.     closeserial();
  438.  
  439.     Delay(5);
  440.  
  441.     if (debug & 128) printf(">Trying to re-establish contact at %d\n",BAUDRATE);
  442.     openserial(BAUDRATE);
  443.     flushserial(buffer,512,show);
  444.     sendchar(0x05);
  445.     sendchar(0x05);
  446.     flushserial(buffer,512,show);
  447.     if (buffer[0]==0x6 && buffer[1]==0x6)
  448.     {
  449.         if (debug & 128) printf(" >Connected succesfully at %d\n",BAUDRATE);
  450.         return(1);
  451.     }
  452.     else
  453.     {
  454.         if (debug & 128) printf(" >FAILED\n");
  455.         return(0);
  456.      }
  457.     return(1);
  458. }
  459.